from fastapi import APIRouter, HTTPException
from typing import Dict, Any
from app.trainer.ml_trainer import MLTrainer

router = APIRouter()
trainer = MLTrainer()

@router.post("/train")
async def train_model(params: Dict[str, Any]):
    """启动模型训练的端点"""
    result = await trainer.start_training(params)
    if result["status"] == "error":
        raise HTTPException(status_code=400, detail=result["message"])
    return result

@router.get("/status")
async def get_training_status():
    """获取训练状态的端点"""
    return trainer.get_status()

@router.get("/metrics")
async def get_training_metrics():
    """获取详细训练指标的端点"""
    status = trainer.get_status()
    if not status["history"]:
        raise HTTPException(status_code=404, detail="No training history available")
    
    return {
        "status": status["status"],
        "metrics": {
            "loss_history": status["history"]["loss"],
            "accuracy_history": status["history"]["accuracy"],
            "timestamps": status["history"]["timestamps"]
        },
        "log_file": status["log_file"]
    } 